home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 11.7 KB | 424 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UAssociation.cp
- // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UASSOCIATION__
- #include "UAssociation.h"
- #endif
-
- // MacApp
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #ifndef __UITERATOR__
- #include "UIterator.h"
- #endif
-
- #ifndef __ULISTITERATOR__
- #include "UListIterator.h"
- #endif
-
- #ifndef __UCOREUTILITIES__
- #include "UCoreUtilities.h"
- #endif
-
- // Toolbox
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- static CompareResult CompareEntryKeys(TObject* anItem, void* yourDataPtr);
-
- //========================================================================================
- // struct CEntryWithKey
- //========================================================================================
-
- struct CEntryWithKey
- {
- public:
- // Fields
- const CStr255& fKeyStr;
-
- // Constructor
- CEntryWithKey(const CStr255& thekeyStr) :
- fKeyStr(thekeyStr)
- {
- }
- };
-
-
- //----------------------------------------------------------------------------------------
- // CompareEntryKeys:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- CompareResult CompareEntryKeys(TObject* anItem, void* yourDataPtr)
- {
- CEntryWithKey* comparisonInfo = (CEntryWithKey*)yourDataPtr;
-
- if (comparisonInfo->fKeyStr < **(((TEntry *)anItem)->fKey))
- return kItemGreaterThanCriteria;
- else if (comparisonInfo->fKeyStr > **(((TEntry *)anItem)->fKey))
- return kItemLessThanCriteria;
- else
- return kItemEqualCriteria;
- } // CompareEntryKeys
-
-
- //========================================================================================
- // CLASS TEntry
- //========================================================================================
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment MAAssociationNonRes
- MA_DEFINE_CLASS_M1(TEntry, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TEntry constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- TEntry::TEntry()
- {
- fKey = NULL;
- fValue = NULL;
- } // TEntry::TEntry
-
- //----------------------------------------------------------------------------------------
- // TEntry::IEntry:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TEntry::IEntry(const CStr255& itsKey,
- const CStr255& itsValue)
- {
- this->IObject();
-
- FailInfo fi;
- Try(fi)
- {
- fKey = (CStringHandle)NewString(itsKey); // need NewPermString
- FailNIL(fKey);
-
- fValue = (CStringHandle)NewString(itsValue);
- FailNIL(fValue);
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- } // TEntry::IEntry
-
- //----------------------------------------------------------------------------------------
- // TEntry::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- TEntry::~TEntry()
- {
- fKey = (CStringHandle)DisposeIfHandle((Handle)fKey);
- fValue = (CStringHandle)DisposeIfHandle((Handle)fValue);
- } // TEntry::Free
-
- //----------------------------------------------------------------------------------------
- // TEntry::SetValue:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TEntry::SetValue(const CStr255& value)
- {
- SetString((StringHandle)fValue, (ConstStr255Param)&value);
- if ((**fValue) != value)
- FailOSErr(memFullErr); // SetString may attempt to increase the
- // handle size, yet it does not return
- // errors!
- } // TEntry::SetValue
-
-
- //========================================================================================
- // CLASS TEntriesList
- //========================================================================================
- #undef Inherited
- #define Inherited TSortedList
-
- #pragma segment MAAssociationNonRes
- MA_DEFINE_CLASS_M1(TEntriesList, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TEntriesList: Empty constructor to satisfy the compiler.
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- TEntriesList::TEntriesList()
- {
- } // TEntriesList::TEntriesList
-
- //----------------------------------------------------------------------------------------
- // TEntriesList destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TEntriesList::~TEntriesList()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TEntriesList::Compare:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- CompareResult TEntriesList::Compare(TObject* item1,
- TObject* item2)
- {
- if ((**((TEntry *)item1)->fKey) < (**((TEntry *)item2)->fKey))
- return kItem1LessThanItem2;
- else if ((**((TEntry *)item1)->fKey) > (**((TEntry *)item2)->fKey))
- return kItem1GreaterThanItem2;
- else
- return kItem1EqualItem2;
- } // TEntriesList::Compare
-
- //----------------------------------------------------------------------------------------
- // TEntriesList::IEntriesList:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TEntriesList::IEntriesList()
- {
- this->ISortedList();
- } // TEntriesList::IEntriesList
-
-
-
- //========================================================================================
- // CLASS TAssociation
- //========================================================================================
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment MAAssociationNonRes
- MA_DEFINE_CLASS_M1(TAssociation, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TAssociation constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- TAssociation::TAssociation()
- {
- fEntries = NULL;
- } // TAssociation::TAssociation
-
- //----------------------------------------------------------------------------------------
- // TAssociation::IAssociation:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TAssociation::IAssociation()
- {
- this->IObject();
-
- FailInfo fi;
- Try(fi)
- {
- TEntriesList * anEntriesList;
- anEntriesList = new TEntriesList;
- anEntriesList->IEntriesList();
- fEntries = anEntriesList;
- #if qDebug
- fEntries->SetEltType("TEntry");
- #endif
-
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- } // TAssociation::IAssociation
-
- //----------------------------------------------------------------------------------------
- // TAssociation::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- TAssociation::~TAssociation()
- {
- fEntries = (TEntriesList *)FreeListIfObject(fEntries);
- } // TAssociation::Free
-
- //----------------------------------------------------------------------------------------
- // TAssociation::EntryWithKey:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- TEntry* TAssociation::EntryWithKey(const CStr255& keyStr)
- {
- CEntryWithKey aCEntryWithKey(keyStr);
-
- return (TEntry *)(fEntries->Search(&CompareEntryKeys, &aCEntryWithKey));
- } // TAssociation::EntryWithKey
-
- //----------------------------------------------------------------------------------------
- // TAssociation::EntryWithValue:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- TEntry* TAssociation::EntryWithValue(const CStr255& valueStr)
- {
- TEntry* returnEntry = NULL;
- CArrayIterator iter(fEntries);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- {
- TEntry* anEntry = (TEntry*)fEntries->At(i);
-
- if (valueStr == **(anEntry->fValue))
- {
- returnEntry = anEntry;
- break;
- }
- }
- return returnEntry;
- } // TAssociation::EntryWithValue
-
- //----------------------------------------------------------------------------------------
- // TAssociation::InsertEntry:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TAssociation::InsertEntry(const CStr255& keyStr,
- const CStr255& valueStr)
- {
- TEntry * anEntry;
-
- anEntry = this->EntryWithKey(keyStr);
- if (anEntry)
- {
- anEntry->SetValue(valueStr);
- }
- else
- {
- anEntry = new TEntry;
- anEntry->IEntry(keyStr, valueStr);
- fEntries->Insert(anEntry);
- }
- } // TAssociation::InsertEntry
-
- //----------------------------------------------------------------------------------------
- // TAssociation::KeyAt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- Boolean TAssociation::KeyAt(const CStr255& valueStr,
- CStr255& keyStr)
- {
- Boolean returnVal = FALSE;
- keyStr.Empty();
-
- TEntry * theEntry = NULL;
- CArrayIterator iter(fEntries);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- {
- TEntry* anEntry = (TEntry*)fEntries->At(i);
-
- if (valueStr == **(anEntry->fValue))
- {
- theEntry = anEntry;
- break;
- }
- }
-
- if (theEntry)
- {
- keyStr = **((String255Handle)(theEntry->fKey));
- returnVal = TRUE;
- }
-
- return returnVal;
- } // TAssociation::KeyAt
-
- //----------------------------------------------------------------------------------------
- // TAssociation::RemoveValueAt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TAssociation::RemoveValueAt(const CStr255& keyStr)
- {
- TEntry * theEntry;
- CEntryWithKey aCEntryWithKey(keyStr);
-
- theEntry = (TEntry *)(fEntries->Search(&CompareEntryKeys, &aCEntryWithKey));
- if (theEntry)
- fEntries->Delete(theEntry);
- } // TAssociation::RemoveValueAt
-
- //----------------------------------------------------------------------------------------
- // TAssociation::RemoveKeyAt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- void TAssociation::RemoveKeyAt(const CStr255& valueStr)
- {
- TEntry * theEntry = NULL;
- CArrayIterator iter(fEntries);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- {
- TEntry* anEntry = (TEntry*)fEntries->At(i);
-
- if (valueStr == **(anEntry->fValue))
- {
- theEntry = anEntry;
- break;
- }
- }
- if (theEntry)
- fEntries->Delete(theEntry);
- } // TAssociation::RemoveKeyAt
-
- //----------------------------------------------------------------------------------------
- // TAssociation::ValueAt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAAssociationRes
-
- Boolean TAssociation::ValueAt(const CStr255& keyStr,
- CStr255& valueStr)
- {
- Boolean returnVal = FALSE;
- valueStr.Empty();
-
- CEntryWithKey aCEntryWithKey(keyStr);
-
- TEntry * theEntry = (TEntry *)(fEntries->Search(&CompareEntryKeys, &aCEntryWithKey));
- if (theEntry)
- {
- valueStr = **((String255Handle)(theEntry->fValue));
- returnVal = TRUE;
- }
-
- return returnVal;
- } // TAssociation::ValueAt
-
- //----------------------------------------------------------------------------------------
- // End of UAssociation.cp
-
- #pragma segment Inline
-